package org.dtrd.modules.patient.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.patient.entity.bean.PatientTagDetail;
import org.dtrd.modules.content.tags.entity.bean.TagDetail;
import org.dtrd.modules.patient.entity.po.DtrdRlPatientTag;
import org.dtrd.modules.patient.mapper.DtrdRlPatientTagMapper;
import org.dtrd.modules.patient.service.IDtrdRlPatientTagService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 患者标签关系表 服务实现类
 * </p>
 *
 * @author QR
 * @since 2022-03-08
 */
@DS("multi-datasource1")
@Service
public class DtrdRlPatientTagServiceImpl extends ServiceImpl<DtrdRlPatientTagMapper, DtrdRlPatientTag> implements IDtrdRlPatientTagService {

    @Override
    public List<TagDetail> getPatientTags(Integer patientId) {
        return baseMapper.selectPatientTags(patientId);
    }

    @Override
    public PatientTagDetail getPatientTag(Integer dataId) {
        DtrdRlPatientTag dtrdRlPatientTag = getById(dataId);
        return Optional.ofNullable(dtrdRlPatientTag)
                        .map(po -> new PatientTagDetail().parseFromPO(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdatePatientTag(PatientTagDetail dto) {
        DtrdRlPatientTag dtrdRlPatientTag = new DtrdRlPatientTag();
        if(dto.getDataId() == null){
            dtrdRlPatientTag = dtrdRlPatientTag.init();
        }
        dtrdRlPatientTag.parseFromDTO(dto);
        return saveOrUpdate(dtrdRlPatientTag);
    }

    @Override
    public void savePatientTag(Integer patientId, Integer tagId) {
        LambdaUpdateWrapper<DtrdRlPatientTag> wrapper = Wrappers.lambdaUpdate(DtrdRlPatientTag.class)
                .eq(DtrdRlPatientTag::getPatientId, patientId)
                .eq(DtrdRlPatientTag::getTagId, tagId);
        DtrdRlPatientTag patientTag = new DtrdRlPatientTag(patientId, tagId);
        saveOrUpdate(patientTag, wrapper);
    }

    @Override
    public boolean removePatientTag(Integer dataId) {
        return removeById(dataId);
    }

    @Override
    public boolean saveOrUpdatePatientTagRelation(Integer patientId, List<Integer> tagIds) {
        // 先删掉原来的记录
        removeByPatientId(patientId);
        // tagIds 为空就返回 true 成功
        if (CollectionUtil.isEmpty(tagIds)) {
            return true;
        }
        // 保存新的记录
        List<DtrdRlPatientTag> patientTags = tagIds.stream().map(t -> new DtrdRlPatientTag(patientId, t))
                .collect(Collectors.toList());
        return saveBatch(patientTags);
    }

    private void removeByPatientId(Integer patientId) {
        LambdaQueryWrapper<DtrdRlPatientTag> wrapper = Wrappers.lambdaQuery(DtrdRlPatientTag.class)
                .eq(DtrdRlPatientTag::getPatientId, patientId);
        remove(wrapper);
    }

    private LambdaQueryWrapper<DtrdRlPatientTag> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdRlPatientTag.class)
                        .eq(DtrdRlPatientTag::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdRlPatientTag> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdRlPatientTag.class)
                        .eq(DtrdRlPatientTag::getIsDel, 0);
    }
}
